/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClassFrame;

import GamePlay.CanvasGame;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Black Diamond
 */
public class Popup {

    private int id;
    private int x, y, xCenter, yCenter;
    private int bg;
    private int content = -1;
    private String positiveBtn = null;
    private String negativeBtn = null;
    private PopupOnclickListener listener;
    private Button btnPositive, btnNegative;
    private boolean isDraw = false;
    private CanvasGame canvas;
    private static CFont Font = null;
    private Content contentObj;

    public Popup() {
    }

    /**
     * 
     * @param canvas
     * @param listener
     * @param bg
     * @param content
     * @param positiveBtn
     * @param negativeBtn = null if you just need one button
     */
    public Popup(int id, CanvasGame canvas, PopupOnclickListener listener, int bg, int content, String positiveBtn, String negativeBtn) {
        this.id = id;
        this.canvas = canvas;
        this.listener = listener;
        this.bg = bg;
        this.content = content;
        this.positiveBtn = positiveBtn;
        this.negativeBtn = negativeBtn;
        int bgw = canvas.getResource().getIMG(bg).getWidth();
        int bgh = canvas.getResource().getIMG(bg).getHeight();
        this.x = (CanvasGame.wScreen - bgw) / 2;
        this.y = (CanvasGame.hScreen - bgh) / 2;
        this.xCenter = x + bgw / 2;
        this.yCenter = y + bgh / 2;
        if (negativeBtn != null) {
            btnPositive = new Button(positiveBtn, Resource.IMG_BTN_LT, x + bgw * 3 / 4 - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getWidth() / 2, y + bgh - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getHeight() / 3);
            btnNegative = new Button(negativeBtn, Resource.IMG_BTN_LT, x + bgw / 4 - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getWidth() / 2, y + bgh - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getHeight() / 3);
        } else {
            btnPositive = new Button(positiveBtn, Resource.IMG_BTN_LT, (CanvasGame.wScreen - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getWidth()) / 2, y + bgh - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getHeight() / 3);
        }
    }

    public Popup(int id, CanvasGame canvas, PopupOnclickListener listener, int bg, String content, String positiveBtn, String negativeBtn) {
        this.id = id;
        Font = new CFont("/font/f.png",
                new byte[]{
                    7, 6, 7, 7, 7, 7, 7, 7, 7, 7,
                    4, 4, 4, 4, 8, 6, 6, 6, 5, 0, 8, 8, 8, 8, //.,:!?()+*|>/-%
                    7, 8, 6, 8, 7, 5, 8, 8, 4, 6,//abcdefghij
                    7, 4, 10, 8, 7, 7, 7, 6, 6, 5,//klmnopqrst
                    7, 7, 10, 7, 8, 6, 7, 7, 7, 7,//uvwxyzáàảã
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,//ạăắằẳẵặâấầ
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,//ẩẫậéèẻẽẹêế
                    8, 8, 8, 8, 5, 5, 5, 5, 5, 8,//ềểễệíìỉĩịóò
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                    7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                    7, 7, 7,
                    8, 8, 8, 8, 6, 6, 8, 8, 4, 6, //ABCDEFGHIJ
                    7, 6, 11, 9, 8, 8, 8, 8, 8, 7,//KLMNOPQRST
                    8, 8, 11, 8, 8, 7, 8, 8, 8,//UVWXYZĐ$Ă
                },
                13, 3, "0123456789.,:!?()+*|>/-%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZĐ$Ă");
        this.canvas = canvas;
        this.listener = listener;
        this.bg = bg;
        this.contentObj = new Content();
        contentObj.splitContent(content, Font, 200, 10);
        this.positiveBtn = positiveBtn;
        this.negativeBtn = negativeBtn;
        int bgw = canvas.getResource().getIMG(bg).getWidth();
        int bgh = canvas.getResource().getIMG(bg).getHeight();
        this.x = (CanvasGame.wScreen - bgw) / 2;
        this.y = (CanvasGame.hScreen - bgh) / 2;
        this.xCenter = x + bgw / 2;
        this.yCenter = y + bgh / 2;

        if (negativeBtn != null) {
            btnPositive = new Button(positiveBtn, Resource.IMG_BTN_LT, x + bgw * 3 / 4 - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getWidth() / 2, y + bgh - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getHeight() / 3);
            btnNegative = new Button(negativeBtn, Resource.IMG_BTN_LT, x + bgw / 4 - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getWidth() / 2, y + bgh - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getHeight() / 3);
        } else {
            btnPositive = new Button(positiveBtn, Resource.IMG_BTN_LT, (CanvasGame.wScreen - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getWidth()) / 2, y + bgh - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getHeight() / 3);
        }
    }

    public Popup(int id, CanvasGame canvas, PopupOnclickListener listener, int x, int y, int bg, int content, String positiveBtn, String negativeBtn) {
        this.id = id;
        this.canvas = canvas;
        this.listener = listener;
        this.x = x;
        this.y = y;
        this.xCenter = x + canvas.getResource().getIMG(bg).getWidth() / 2;
        this.yCenter = y + canvas.getResource().getIMG(bg).getHeight() / 2;
        this.bg = bg;
        this.content = content;
        this.positiveBtn = positiveBtn;
        this.negativeBtn = negativeBtn;
        if (negativeBtn != null) {
            btnPositive = new Button(positiveBtn, Resource.IMG_BTN_LT, x + canvas.getResource().getIMG(bg).getWidth() * 3 / 4 - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getWidth() / 2, y + canvas.getResource().getIMG(bg).getHeight() - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getHeight() / 3);
            btnNegative = new Button(negativeBtn, Resource.IMG_BTN_LT, x + canvas.getResource().getIMG(bg).getWidth() / 4 - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getWidth() / 2, y + canvas.getResource().getIMG(bg).getHeight() - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getHeight() / 3);
        } else {
            btnPositive = new Button(positiveBtn, Resource.IMG_BTN_LT, x + canvas.getResource().getIMG(bg).getWidth() / 2 - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getWidth() / 2, y + canvas.getResource().getIMG(bg).getHeight() - canvas.getResource().getIMG(Resource.IMG_BTN_LT).getHeight() / 3);
        }
    }

    public int getBg() {
        return bg;
    }

    public void setBg(int bg) {
        this.bg = bg;
    }

    public int getContent() {
        return content;
    }

    public int getID() {
        return id;
    }

    public void setContent(int content) {
        this.content = content;
    }

    public PopupOnclickListener getListener() {
        return listener;
    }

    public void setListener(PopupOnclickListener listener) {
        this.listener = listener;
    }

    public void show() {
        this.isDraw = true;
    }

    public boolean isShowing() {
        return this.isDraw;
    }

    public void draw(Graphics g) {
        if (isDraw) {
            g.drawImage(canvas.getResource().getIMG(bg), x, y, Graphics.TOP | Graphics.LEFT);
            if (content != -1) {
                g.drawImage(canvas.getResource().getIMG(content), xCenter - canvas.getResource().getIMG(content).getWidth() / 2, yCenter - canvas.getResource().getIMG(content).getHeight() / 2, Graphics.TOP | Graphics.LEFT);
            } else {
                Font.Render(g, contentObj.getStrResult(), xCenter, yCenter - contentObj.getHeight() / 2, CFont.ALIGN_CENTER);
            }
            btnPositive.drawButton(g, canvas);
            if (btnNegative != null) {
                btnNegative.drawButton(g, canvas);
            }
        }
    }

    public void dismiss() {
        isDraw = false;
    }

    public void pointerEvent(int x, int y, int event) {
//        System.out.println("VKL");
        switch (event) {
            case InputKey.POINTER_EVENT_DOWN:
                if (canvas.checkButton(x, y, btnPositive)) {
                    btnPositive.setState(Button.HOLD_CLICK);
                    canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
//                    System.out.println("VKL1");
                } else if (btnNegative != null && canvas.checkButton(x, y, btnNegative)) {
                    btnNegative.setState(Button.HOLD_CLICK);
                    canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
//                    System.out.println("VKL2");
                }
                break;
            case InputKey.POINTER_EVENT_UP:
                if (canvas.checkButton(x, y, btnPositive) && btnPositive.getState() == Button.HOLD_CLICK) {
                    listener.onClickPositiveButton(id);
                } else if (btnNegative != null && canvas.checkButton(x, y, btnNegative) && btnNegative.getState() == Button.HOLD_CLICK) {
                    listener.onClickNegativeButton(id);
                    btnNegative.setState(Button.ENABLE);
                }
                btnPositive.setState(Button.ENABLE);
                break;
        }
    }

    public class Content {

        private String str;
        private String strResult;
        private Vector lines;
        private CFont font;

        public Content() {
        }

        public Content(String str, CFont font) {
            this.str = str;
            this.font = font;
        }

        public void splitContent(String str, CFont font, int printWith, int maxRow) {
            this.str = str;
            this.font = font;
            this.lines = Utils.splitText(str, font, printWith, maxRow);
            this.strResult = normalizationQuestion(lines);
        }

        public String normalizationQuestion(Vector v) {
            String result = "";
            if (v.size() > 0) {
                for (int loop = 0; loop < v.size(); loop++) {
                    result = result + (String) v.elementAt(loop)
                            + "\n";
                }
            }
            return result;
        }

        public int getHeight() {
            return (int) lines.size() * font.GetHeight();
        }

        public CFont getFont() {
            return font;
        }

        public void setFont(CFont font) {
            this.font = font;
        }

        public Vector getLines() {
            return lines;
        }

        public void setLines(Vector lines) {
            this.lines = lines;
        }

        public String getStr() {
            return str;
        }

        public void setStr(String str) {
            this.str = str;
        }

        public String getStrResult() {
            return strResult;
        }

        public void setStrResult(String strResult) {
            this.strResult = strResult;
        }
    }
}
